package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.ISysMenuService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleVsMenuService;
import com.jf.cloud.service.sysmanager.entity.SysMenu;
import com.jf.cloud.service.sysmanager.entity.SysRole;
import com.jf.cloud.service.sysmanager.entity.SysRoleVsMenu;
import com.jf.cloud.service.sysmanager.entity.SysStaff;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 * 
 * @author LangK
 * @since 2017-12-04
 */
@Service
public class SysRoleServiceImpl extends BaseService<SysRole, Long> implements ISysRoleService {

	@Resource
	private BaseDao baseDao;

	@Resource
	private ISysMenuService menuService;

	@Resource
	private ISysRoleVsMenuService roleMenuService;

	/**
	 * 修改角色对象
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午7:01:21
	 */
	@Transactional
	public void updateRole(SysRole sysRole) {
		String sql = "UPDATE sys_role SET ROLE_NAME = ?, ROLE_TYPE = ?, OPT_TIME = ?," + "ROLE_REMARK = ?, OPT_ID = ?, OPT_ACCOUNT = ? WHERE ROW_ID = ? ";
		this.baseDao.update(sql, sysRole.getRoleName(), sysRole.getRoleType(), sysRole.getOptTime(), sysRole.getRoleRemark(), sysRole.getOptId(), sysRole.getOptAccount(), sysRole.getRowId());
	}

	/**
	 * 删除角色对象
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午7:01:30
	 */
	@Transactional
	public void deleteRole(Long rowId, SysStaff user) {
		String sql = "UPDATE sys_role SET DEL_FLAG = 1, OPT_TIME = ?, " + "OPT_ID = ?, OPT_ACCOUNT = ? WHERE ROW_ID = ?";
		this.baseDao.update(sql, DateUtil.getCurrentDateTime(), user.getRowId(), user.getStaffAccount(), rowId);
	}

	/**
	 * 查询角色权限树
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午7:01:39
	 */
	public List<Map<String, Object>> getMenuListByTree(Long roleId, String currId) throws RuntimeException {

		// 1.查询当前登录用户的菜单权限
		Set<Long> menuIdsCurr = new HashSet<Long>();
		if (!"1".equals(currId)) { // 不是超级管理员
			Query querycurr = new Query();
			querycurr.put("ROLE_ID", currId);
			List<SysRoleVsMenu> roleMenuList = roleMenuService.findList(querycurr);
			for (SysRoleVsMenu roleVsMenu : roleMenuList) {
				menuIdsCurr.add(roleVsMenu.getMenuId());
			}
		}

		List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
		Query query = new Query();
		query.put("ROLE_ID", roleId);
		List<SysRoleVsMenu> roleMenuList = roleMenuService.findList(query);
		Set<Long> menuIds = new HashSet<Long>();
		for (SysRoleVsMenu roleVsMenu : roleMenuList) {
			menuIds.add(roleVsMenu.getMenuId());
		}
		if ("1".equals(currId)) { // 超级管理员
			// 2.查找所有菜单
			List<SysMenu> findList = this.menuService.findMenuList();
			for (SysMenu sysMenu : findList) {
				HashMap<String, Object> hashMap = new HashMap<String, Object>();
				Long id = sysMenu.getRowId();
				hashMap.put("id", id);
				hashMap.put("pId", sysMenu.getParentId());
				hashMap.put("fileUrl", "/menu/info?id=" + id);
				hashMap.put("name", sysMenu.getMenuName());
				hashMap.put("hasChildren", sysMenu.getParentId() != null && sysMenu.getParentId() != 0);
				hashMap.put("menuType", sysMenu.getMenuFlag());
				if (menuIds.contains(id)) {
					hashMap.put("checked", "true");
				}
				menus.add(hashMap);
			}
		} else {
			List<SysMenu> findOneList = this.menuService.getBelongList(0L); // 一级
			for (SysMenu sysMenu1 : findOneList) {
				List<SysMenu> findSecondList = this.menuService.getBelongList(sysMenu1.getRowId()); // 二级
				int num = 0;
				for (SysMenu sysMenu2 : findSecondList) {
					if (menuIdsCurr.contains(sysMenu2.getRowId())) {
						num = 1;
						HashMap<String, Object> hashMap2 = new HashMap<String, Object>();
						Long id2 = sysMenu2.getRowId();
						hashMap2.put("id", id2);
						hashMap2.put("pId", sysMenu2.getParentId());
						hashMap2.put("fileUrl", "/menu/info?id=" + id2);
						hashMap2.put("name", sysMenu2.getMenuName());
						hashMap2.put("hasChildren", sysMenu2.getParentId() != null && sysMenu2.getParentId() != 0);
						hashMap2.put("menuType", sysMenu2.getMenuFlag());
						if (menuIds.contains(id2)) {
							hashMap2.put("checked", "true");
						}
						menus.add(hashMap2);
						List<SysMenu> findThirdList = this.menuService.getBelongList(sysMenu2.getRowId()); // 三级
						for (SysMenu sysMenu3 : findThirdList) {
							if (menuIdsCurr.contains(sysMenu3.getRowId())) {
								HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
								Long id3 = sysMenu3.getRowId();
								hashMap3.put("id", id3);
								hashMap3.put("pId", sysMenu3.getParentId());
								hashMap3.put("fileUrl", "/menu/info?id=" + id3);
								hashMap3.put("name", sysMenu3.getMenuName());
								hashMap3.put("hasChildren", sysMenu3.getParentId() != null && sysMenu3.getParentId() != 0);
								hashMap3.put("menuType", sysMenu3.getMenuFlag());
								if (menuIds.contains(id3)) {
									hashMap3.put("checked", "true");
								}
								menus.add(hashMap3);
							}
						}

					}
				}

				if (num == 1) {
					HashMap<String, Object> hashMap1 = new HashMap<String, Object>();
					Long id = sysMenu1.getRowId();
					hashMap1.put("id", id);
					hashMap1.put("pId", sysMenu1.getParentId());
					hashMap1.put("fileUrl", "/menu/info?id=" + id);
					hashMap1.put("name", sysMenu1.getMenuName());
					hashMap1.put("hasChildren", sysMenu1.getParentId() != null && sysMenu1.getParentId() != 0);
					hashMap1.put("menuType", sysMenu1.getMenuFlag());
					if (menuIds.contains(id)) {
						hashMap1.put("checked", "true");
					}
					menus.add(hashMap1);
				}
			}
		}

		return menus;
	}

	/**
	 * 角色权限修改
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午7:01:50
	 */
	@Transactional
	public void updateAuthority(Long roleId, String menuIds, SysStaff user) {
		// 1.删除原有数据
		this.baseDao.updateRisk(" DELETE FROM `sys_role_vs_menu` WHERE ROLE_ID = ?  ", roleId);

		// 2.批量保存新数据
		String[] menuIdArray = menuIds.split(",");
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO `sys_role_vs_menu`(ROLE_ID,MENU_ID) VALUES");
		List<Object> params = new ArrayList<Object>();
		for (int i = 0; i < menuIdArray.length; i++) {
			if (StringUtil.notNull(menuIdArray[i])) {
				if (i == menuIdArray.length - 1) {
					sql.append(" (?,?)");
				} else {
					sql.append(" (?,?),");
				}
				params.add(roleId);
				params.add(menuIdArray[i]);
			}
		}
		if (params != null && params.size() > 0) {
			this.baseDao.update(sql.toString(), params.toArray());
		}

	}

}
